<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">HW管理系统</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <!-- <svg-icon icon-class="user" /> -->
          <i class="el-icon-user-solid"></i>
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="请输入用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <!-- <svg-icon icon-class="password" /> -->
          <i class="el-icon-unlock"></i>
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <el-form-item prop="status">
        <silder-verify
          @success="handleSuccess"
          @failed="handleError"
        ></silder-verify>
      </el-form-item>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
        >登 录</el-button
      >

      <div class="tips">
        <span @click="toClick">返回首页</span>
        <span @click="reset">忘记密码?</span>
      </div>
    </el-form>
  </div>
</template>

<script>
import { basePath, basePath2 } from "@/common";
import { setToken, setInfo } from "@/utils/auth";
import SilderVerify from "@/components/SilderVerify";

export default {
  name: "Login",
  components: {
    SilderVerify,
  },

  data() {
    const validateUsername = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("用户名不能为空"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码错误"));
      } else {
        callback();
      }
    };
    const validatestatus = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请拖动滑块完成验证"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "admin",
        password: "111111",
        status: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        status: [{ validator: validatestatus, trigger: "change" }],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleSuccess() {
      this.loginForm.status = true;
      this.$refs.loginForm.validateField("status");
    },
    handleError(){},
    reset() {
      this.$router.push("/resetPassword");
    },
    toClick() {
      this.$router.push("/");
    },
    handleLogin() {
      this.loading = true;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = false;
          setToken("999HHAIO");
          setInfo({
            name: "王野",
            circleUrl:
              "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
          });
          let url = "";
          if (process.env.NODE_ENV == "development") {
            url = basePath;
          } else {
            url = basePath2;
          }
          window.location.href = url + "/#/?tk=999HHAIO";
        } else {
          this.loading = false;
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.login-container .el-input {
  display: inline-block;
  height: 47px;
  width: 85%;
  background: transparent;
}
.login-container .el-input input {
  background: transparent;
  border: 0px;
  -webkit-appearance: none;
  border-radius: 0px;
  padding: 12px 5px 12px 15px;
  color: #fff;
  height: 47px;
  caret-color: #fff;
}

/deep/ .el-input input:-webkit-autofill {
  -webkit-box-shadow: 0 0 0px 1000px #283443 inset !important;
  -webkit-text-fill-color: #fff !important;
}

.login-container .el-form-item {
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(0, 0, 0, 0.1) !important;
  border-radius: 5px;
  color: #454545;
}
</style>

<style scoped>
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #2d3a4b;
  overflow: hidden;
}
.login-form {
  position: relative;
  width: 520px;
  max-width: 100%;
  padding: 160px 35px 0;
  margin: 0 auto;
  overflow: hidden;
}

.tips {
  font-size: 14px;
  color: #409eff;
  margin-bottom: 10px;
  text-align: right;
}
.tips:hover {
  cursor: pointer;
}
.tips span:first-of-type {
  margin-right: 16px;
}
.svg-container {
  padding: 6px 5px 6px 15px;
  color: #889aa4;
  vertical-align: middle;
  width: 30px;
  display: inline-block;
}

.title-container {
  position: relative;
}
.title-container .title {
  font-size: 26px;
  color: #eee;
  margin: 0px auto 40px auto;
  text-align: center;
  font-weight: bold;
}
.show-pwd {
  position: absolute;
  right: 10px;
  top: 7px;
  font-size: 16px;
  color: #889aa4;
  cursor: pointer;
  user-select: none;
}
/deep/ .el-input__inner {
  background: rgba(0, 0, 0, 0) !important;
  color: #fff;
  border: none;
}
/deep/ .el-input {
  background: rgba(0, 0, 0, 0) !important;
}
</style>